package prac10;

/**
* implements a word object for user in prac 10, the word can be set, retrieved,
* have its hash value calculated, returned as a string and compared to another
* object
* 
* @author Menghong Li, Qian Zhao
* @since 11-10-2011
* @input none
* @output none
* @errorHandling none
* @knownBugs none
*/

public class Word implements Hashable {
	
	
	private static  int tableSize = 97;
	private static  int prime = 53;
	private String word;
	
	
	
	/**
	 * @return the tableSize
	 */
	public static int getTableSize() {
		return tableSize;
	}



	/**
	 * @param tableSize the tableSize to set
	 */
	public static void setTableSize(int tableSize) {
		tableSize = tableSize;
	}



	/**
	 * @return the prime
	 */
	public static int getPrime() {
		return prime;
	}



	/**
	 * @param prime the prime to set
	 */
	public static void setPrime(int prime) {
		Word.prime = prime;
	}



	/**
	 * 
	 * @return the word
	 */
	protected String getWord() {
		return word;
	}



	/**
     * Constructs a word
     * 
     * @complexity	O(1) for best and worst case
     * @param		word the value of the word
     */
	public Word(String word){
		this.word = word;
	}
	
	/**
     * calculates the hash for the value of this word
     * 
     * @return		the hash of this word
     * @complexity	best and worst case: O(N)
     *              where N is the number of letters in the word
     */
	public int hash() {
		int value = 0;
		
		for (int i =0; i < word.length(); i++) {
			value = (word.charAt(i) + prime*value) % tableSize; 		}
		return value;
		
	}
	
	public int hash(int tSize, int p) {
		int value = 0;
		
		for (int i =0; i < word.length(); i++) {
			value = (word.charAt(i) + p*value) % tSize; 		}
		return value;
		
	}
	
//	public  int hash() {
//		int value = 0, a = 31415, b = 27183;
//		for (int i = 0; i < word.length(); i++) {
//			value = (word.charAt(i) + a*value) % TABLESIZE;
//			a = a * b % (TABLESIZE-1);
//		}
//		return value;
//	}

	/**
     * returns the word as a string
     * 
     * @return		the word
     * @complexity	best and worst case: O(1).
     */
	public String toString(){
		return this.getWord();
	}
	
	
    /**
     * checks whether this word is equal to another provided object
     * 
     * @param		otherObject 	other object to check against
     * @return		true is equal else false
     * @complexity	best and worst case: O(G + E) 
     *      		where G is the complexity of the {@link Object#getClass() }
     *      			  E is the complexity of {@link Object#equals(java.lang.Object) }
     */
	public boolean equals(Object otherObject){
		if (otherObject == null || otherObject.getClass() != this.getClass()) {
            return false;
        } else {
        	Word otherWord = (Word) otherObject;
        	return (this.getWord().equals(otherWord.getWord()));        
        	}
	}



	
}
